function getUserValidationExpertise(uid) {
  var params = new Object();
  params.uid = uid;

  var request = new Object();
  request.action = 'getUserValidationExpertise';
  request.params = params;

  var url = Drupal.settings.basePath + 'observation/data';
  jQuery.get(
    url,
    {request: JSON.stringify(request)},
    function(response) {
      if (parseInt(response.error) == 1) {
        displayMessage('error', response.msg);
        return;
      } else {
        updateUIWithData(response.data);
      }
    },
    'json'
  );
}

function getMappingHTML(info) {
  var loc = '<div style="border: 1px solid rgb(217, 234, 245); padding: 2px; margin: 2px;">';
  loc += info;
  loc += '</div>';
  return loc;
}

function updateUIWithData(data) {
  if (data.language) {
    var languages = data.language;
    var len = languages.length;
    var $divLanguages = jQuery('#divLanguages');
    for (var i = 0; i < len; i++) {
      $divLanguages.append(getMappingHTML(languages[i]));
    }
  }

  if (data.taxanomy) {
    var taxanomy = data.taxanomy;
    len = taxanomy.length;
    var $divTaxanomies = jQuery('#divTaxanomies');
    for (var i = 0; i < len; i++) {
      $divTaxanomies.append(getMappingHTML(taxanomy[i]));
    }
  }

  if (data.ecosystem) {
    var ecosystem = data.ecosystem;
    len = ecosystem.length;
    var $divEcosystems = jQuery('#divEcosystems');
    for (var i = 0; i < len; i++) {
      $divEcosystems.append(getMappingHTML(ecosystem[i]));
    }
  }

  if (data.waterbody) {
    var waterbodies = data.waterbody;
    len = waterbodies.length;
    var $divWaterbodies = jQuery('#divWaterbodies');
    for (var i = 0; i < len; i++) {
      $divWaterbodies.append(getMappingHTML(waterbodies[i]));
    }
  }

  if (data.location) {
    var location = data.location;
    len = location.length;
    var $divLocations = jQuery('#divLocations');
    for (var i = 0; i < len; i++) {
      $divLocations.append(getMappingHTML(location[i]));
    }
  }
}

function updateStatus() {
  var data = new Object();
  data.uid = jQuery('#uid').val();
  data.status = jQuery('select[name=status]').val();
  data.comments = jQuery('textarea[name=comments]').val();

  var request = new Object();
  request.action = 'updateValidationRequestStatus';
  request.params = data;

  var url = Drupal.settings.basePath + 'observation/data';
  jQuery.get(
    url,
    {request: JSON.stringify(request)},
    function(response) {
      if (parseInt(response.error) == 1) {
        displayMessage('error', response.msg);
        return;
      } else {
        displayMessage('status', response.msg);
        return;
      }
    },
    'json'
  );
}

jQuery(document).ready(function() {
  if (uid) {
    getUserValidationExpertise(uid);
  }

  var $btn = jQuery('#btnUpdateStatus');
  if ($btn.length > 0) {
    $btn.click(updateStatus);
  }
});